﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using SZSLSystem.Common;

namespace SZSLSystem.Web.Admin
{
    public partial class login : System.Web.UI.Page
    {
        private readonly SZSLSystem.BLL.Administrator bll = new SZSLSystem.BLL.Administrator();

        protected void Page_Load(object sender, EventArgs e)
        {

        }

        /// <summary>
        /// 管理员登录
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void loginsubmit_Click(object sender, ImageClickEventArgs e)
        {
            string UserName = this.txtUserName.Text.Trim();
            string UserPwd = this.txtUserPwd.Text.Trim();
            if (string.IsNullOrEmpty(UserName) || string.IsNullOrEmpty(UserPwd))
            {
                lbMsg.Text = "请输入您要登录用户名或密码";
            }
            else
            {
                if (Session["AdminLoginSun"] == null)
                {
                    Session["AdminLoginSun"] = 1;
                }
                else
                {
                    Session["AdminLoginSun"] = Convert.ToInt32(Session["AdminLoginSun"]) + 1;
                }
                //
                //判断登录
                //
                if (Session["AdminLoginSun"] != null && Convert.ToInt32(Session["AdminLoginSun"]) > 3)
                {
                    lbMsg.Text = "登录错误超过3次，请关闭浏览器重新登录。";
                }
                else if (bll.GetModel(UserName) != null)
                {
                    UserPwd = DESEncrypt.Encrypt(UserPwd, bll.GetModel(UserName).Salt);
                    if (bll.chkAdminLogin(UserName, UserPwd))
                    {
                        SZSLSystem.Model.Administrator model = new SZSLSystem.Model.Administrator();
                        model = bll.GetModel(UserName);
                        //
                        //写入Session值
                        //
                        Session["AdminNo"] = model.ID;
                        Session["AdminName"] = model.UserName;
                        Session["AdminRName"] = model.RealName;
                        //
                        //设置Session超时时间
                        //
                        Session.Timeout = 30;
                        Session["AdminLoginSun"] = null;
                        //
                        //写入Cookies
                        //
                        Utils.WriteCookie("AdminID", "SZSLSystem", DESEncrypt.Encrypt(model.ID.ToString()));
                        Utils.WriteCookie("AdminName", "SZSLSystem", DESEncrypt.Encrypt(model.UserName));
                        Utils.WriteCookie("AdminRName", "SZSLSystem", DESEncrypt.Encrypt(model.RealName));
                        Utils.WriteCookie("AdminPwd", "SZSLSystem", model.UserPassword);
                        //
                        //记录日志
                        //
                        new SZSLSystem.Web.UI.ManagePage().SaveLogs(model.ID, "[用户登录]状态：登录成功！", 0);
                        //
                        //页面跳转
                        //
                        Response.Redirect("Admin_index.aspx");
                    }
                    else
                    {
                        lbMsg.Text = "您输入的用户名或密码不正确";
                    }
                }
                else
                {
                    lbMsg.Text = "您输入的用户名或密码不正确";
                }
            }
        }
    }
}
